package com.qdu.servlet;


import com.qdu.impl.ShoppingCarImpl;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class GouWu extends HttpServlet {

    public final static String CONTENTTYPE = "text/html;charset=gb2312";
    public final static String CHARACTERENCODING = "gb2312";


    public GouWu() {
        super();
    }


    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding(this.CHARACTERENCODING);
        response.setContentType(this.CONTENTTYPE);
        String method = request.getParameter("method").trim();
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        String id = request.getParameter("id").trim();
        ShoppingCarImpl sc = new ShoppingCarImpl();
        if (name == null) {
            if (method.equals("book")) {
                request.setAttribute("message", "请先登陆后在进行购物！");
                request.setAttribute("id", id);
                request.getRequestDispatcher("admin/bookinfo.jsp").forward(request, response);
            } else if (method.equals("search")) {
                request.setAttribute("message", "请先登陆后在进行购物！");
                request.setAttribute("id", id);
                request.getRequestDispatcher("search.jsp").forward(request, response);
            }
        } else {
            if (method.equals("book")) {
                int i = sc.addGouWuChe(Integer.parseInt(id), name);
                if (i == 1) {
                    request.setAttribute("message", "操作成功！");
                    request.setAttribute("id", id);
                    request.getRequestDispatcher("admin/bookinfo.jsp").forward(request, response);

                } else {
                    request.setAttribute("message", "系统维护中，请稍后再试！");
                    request.setAttribute("id", id);
                    request.getRequestDispatcher("admin/bookinfo.jsp").forward(request, response);

                }
            }  else if (method.equals("delgw")) {
                int i = sc.delGouWu(Integer.parseInt(id));
                if (i == 1) {
                    request.setAttribute("message", "操作成功！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                } else {
                    request.setAttribute("message", "系统维护中，请稍后再试！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                }
            } else if (method.equals("sendorder")) {
                long number = new java.util.Date().getTime();
                String fukuanfangshi = request.getParameter("fukuanfangshi").trim();
                int i = sc.addOrder(number, name, fukuanfangshi);
                if (i == 1) {
                    request.setAttribute("message", "操作成功！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                } else {
                    request.setAttribute("message", "系统维护中，请稍后再试！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                }
            } else if (method.equals("deldd")) {
                int i = sc.delDD(Integer.parseInt(id));
                if (i == 1) {
                    request.setAttribute("message", "操作成功！");
                    request.getRequestDispatcher("dingdan.jsp").forward(request, response);

                } else {
                    request.setAttribute("message", "系统维护中，请稍后再试！");
                    request.getRequestDispatcher("dingdan.jsp").forward(request, response);

                }
            } else if (method.equals("upgouwu")) {
                String shuliang = request.getParameter("shuliang").trim();
                int i = sc.upGouWuChe(Integer.parseInt(id), Integer.parseInt(shuliang), name);
                if (i == 1) {
                    request.setAttribute("message", "操作成功！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                } else {
                    request.setAttribute("message", "系统维护中，请稍后再试！");
                    request.getRequestDispatcher("gouwuche.jsp").forward(request, response);

                }
            }
        }

    }

    public void init() throws ServletException {
    }

}
